#include "UdpServer.h"

#ifdef __linux__
bool UdpServer::Create(const Desc &desc)
{
    struct sockaddr_in servaddr, cliaddr; // 定义服务器与客户端地址结构体
    socklen_t cliaddr_len;                // 客户端地址长度
    sock = socket(desc.af, desc.type, desc.protocol);
    LOGI("sock %d", sock);
    if (sock == -1)
    {
        LOGI("socket failed return %d", socket);
        return false;
    }
    // memset(&servaddr, 0, sizeof(servaddr)); // 将服务器端口地址清零
    servaddr.sin_family = desc.af;
    servaddr.sin_addr.s_addr = htonl(desc.sAddr);
    servaddr.sin_port = htons(desc.port);
    int bind_res = bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));

    // LOGI("bind res %d", bind_res);
    if (bind_res != 0)
    {
        LOGI("cannot bind %d", desc.sAddr, desc.port);
        return false;
    }

    LOGI("Create udp server on %d successfully", desc.port);

    return true;
}

void UdpServer::Close()
{
    close(sock);
}

#endif // DEBUG
